#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" By Martin Senande-Rivera
    For Spatial and temporal expansion of global wildland fire activity in response to climate change """

import numpy as np
import pandas as pd
import xarray as xr

### File reading ###

ruta_KG='../1-Threshold_selection/'  
ds_kg = xr.open_dataset(ruta_KG+'KG_class.nc')
KG = ds_kg.KG 						# KG classification

# Paths of each General Climate Model output
ruta_ACCESS1_0='./GCMs/ACCESS1-0/'
ruta_ACCESS1_3='./GCMs/ACCESS1-3/'
ruta_BCC_CSM1_1='./GCMs/BCC-CSM1-1/'
ruta_BCC_CSM1_1_M='./GCMs/BCC-CSM1-1-M/'
ruta_BNU_ESM='./GCMs/BNU-ESM/'
ruta_CMCC_CMS='./GCMs/CMCC-CMS/'
ruta_CNRM_CM5='./GCMs/CNRM-CM5/'
ruta_GFDL_CM3='./GCMs/GFDL-CM3/'
ruta_GFDL_ESM2G='./GCMs/GFDL-ESM2G/'
ruta_GFDL_ESM2M='./GCMs/GFDL-ESM2M/'
ruta_GISS_E2_H='./GCMs/GISS-E2-H/'
ruta_GISS_E2_H_CC='./GCMs/GISS-E2-H-CC/'
ruta_GISS_E2_R='./GCMs/GISS-E2-R/'
ruta_GISS_E2_R_CC='./GCMs/GISS-E2-R-CC/'
ruta_HADGEM2_CC='./GCMs/HADGEM2-CC/'
ruta_INMCM4='./GCMs/INMCM4/'
ruta_IPSL_CM5A_LR='./GCMs/IPSL-CM5A-LR/'
ruta_IPSL_CM5A_MR='./GCMs/IPSL-CM5A-MR/'
ruta_IPSL_CM5B_LR='./GCMs/IPSL-CM5B-LR/'
ruta_MPI_ESM_LR='./GCMs/MPI-ESM-LR/'
ruta_MPI_ESM_MR='./GCMs/MPI-ESM-MR/'
ruta_NorESM1_M='./GCMs/NorESM1-M/'

Years_01 = xr.open_dataset(ruta_ACCESS1_0+'Years_FC.nc')
Years_02 = xr.open_dataset(ruta_ACCESS1_3+'Years_FC.nc')
Years_03 = xr.open_dataset(ruta_BCC_CSM1_1+'Years_FC.nc')
Years_04 = xr.open_dataset(ruta_BCC_CSM1_1_M+'Years_FC.nc')
Years_05 = xr.open_dataset(ruta_BNU_ESM+'Years_FC.nc')
Years_06 = xr.open_dataset(ruta_CMCC_CMS+'Years_FC.nc')
Years_07 = xr.open_dataset(ruta_CNRM_CM5+'Years_FC.nc')
Years_08 = xr.open_dataset(ruta_GFDL_CM3+'Years_FC.nc')
Years_09 = xr.open_dataset(ruta_GFDL_ESM2G+'Years_FC.nc')
Years_10 = xr.open_dataset(ruta_GFDL_ESM2M+'Years_FC.nc')
Years_11 = xr.open_dataset(ruta_GISS_E2_H+'Years_FC.nc')
Years_12 = xr.open_dataset(ruta_GISS_E2_H_CC+'Years_FC.nc')
Years_13 = xr.open_dataset(ruta_GISS_E2_R+'Years_FC.nc')
Years_14 = xr.open_dataset(ruta_GISS_E2_R_CC+'Years_FC.nc')
Years_15 = xr.open_dataset(ruta_HADGEM2_CC+'Years_FC.nc')
Years_16 = xr.open_dataset(ruta_INMCM4+'Years_FC.nc')
Years_17 = xr.open_dataset(ruta_IPSL_CM5A_LR+'Years_FC.nc')
Years_18 = xr.open_dataset(ruta_IPSL_CM5A_MR+'Years_FC.nc')
Years_19 = xr.open_dataset(ruta_IPSL_CM5B_LR+'Years_FC.nc')
Years_20 = xr.open_dataset(ruta_MPI_ESM_LR+'Years_FC.nc')
Years_21 = xr.open_dataset(ruta_MPI_ESM_MR+'Years_FC.nc')
Years_22 = xr.open_dataset(ruta_NorESM1_M+'Years_FC.nc')

FC_01 = xr.open_dataset(ruta_ACCESS1_0+'FC.nc')
FC_02 = xr.open_dataset(ruta_ACCESS1_3+'FC.nc')
FC_03 = xr.open_dataset(ruta_BCC_CSM1_1+'FC.nc')
FC_04 = xr.open_dataset(ruta_BCC_CSM1_1_M+'FC.nc')
FC_05 = xr.open_dataset(ruta_BNU_ESM+'FC.nc')
FC_06 = xr.open_dataset(ruta_CMCC_CMS+'FC.nc')
FC_07 = xr.open_dataset(ruta_CNRM_CM5+'FC.nc')
FC_08 = xr.open_dataset(ruta_GFDL_CM3+'FC.nc')
FC_09 = xr.open_dataset(ruta_GFDL_ESM2G+'FC.nc')
FC_10 = xr.open_dataset(ruta_GFDL_ESM2M+'FC.nc')
FC_11 = xr.open_dataset(ruta_GISS_E2_H+'FC.nc')
FC_12 = xr.open_dataset(ruta_GISS_E2_H_CC+'FC.nc')
FC_13 = xr.open_dataset(ruta_GISS_E2_R+'FC.nc')
FC_14 = xr.open_dataset(ruta_GISS_E2_R_CC+'FC.nc')
FC_15 = xr.open_dataset(ruta_HADGEM2_CC+'FC.nc')
FC_16 = xr.open_dataset(ruta_INMCM4+'FC.nc')
FC_17 = xr.open_dataset(ruta_IPSL_CM5A_LR+'FC.nc')
FC_18 = xr.open_dataset(ruta_IPSL_CM5A_MR+'FC.nc')
FC_19 = xr.open_dataset(ruta_IPSL_CM5B_LR+'FC.nc')
FC_20 = xr.open_dataset(ruta_MPI_ESM_LR+'FC.nc')
FC_21 = xr.open_dataset(ruta_MPI_ESM_MR+'FC.nc')
FC_22 = xr.open_dataset(ruta_NorESM1_M+'FC.nc')

PFS_01 = xr.open_dataset(ruta_ACCESS1_0+'PFS.nc')
PFS_02 = xr.open_dataset(ruta_ACCESS1_3+'PFS.nc')
PFS_03 = xr.open_dataset(ruta_BCC_CSM1_1+'PFS.nc')
PFS_04 = xr.open_dataset(ruta_BCC_CSM1_1_M+'PFS.nc')
PFS_05 = xr.open_dataset(ruta_BNU_ESM+'PFS.nc')
PFS_06 = xr.open_dataset(ruta_CMCC_CMS+'PFS.nc')
PFS_07 = xr.open_dataset(ruta_CNRM_CM5+'PFS.nc')
PFS_08 = xr.open_dataset(ruta_GFDL_CM3+'PFS.nc')
PFS_09 = xr.open_dataset(ruta_GFDL_ESM2G+'PFS.nc')
PFS_10 = xr.open_dataset(ruta_GFDL_ESM2M+'PFS.nc')
PFS_11 = xr.open_dataset(ruta_GISS_E2_H+'PFS.nc')
PFS_12 = xr.open_dataset(ruta_GISS_E2_H_CC+'PFS.nc')
PFS_13 = xr.open_dataset(ruta_GISS_E2_R+'PFS.nc')
PFS_14 = xr.open_dataset(ruta_GISS_E2_R_CC+'PFS.nc')
PFS_15 = xr.open_dataset(ruta_HADGEM2_CC+'PFS.nc')
PFS_16 = xr.open_dataset(ruta_INMCM4+'PFS.nc')
PFS_17 = xr.open_dataset(ruta_IPSL_CM5A_LR+'PFS.nc')
PFS_18 = xr.open_dataset(ruta_IPSL_CM5A_MR+'PFS.nc')
PFS_19 = xr.open_dataset(ruta_IPSL_CM5B_LR+'PFS.nc')
PFS_20 = xr.open_dataset(ruta_MPI_ESM_LR+'PFS.nc')
PFS_21 = xr.open_dataset(ruta_MPI_ESM_MR+'PFS.nc')
PFS_22 = xr.open_dataset(ruta_NorESM1_M+'PFS.nc')

### Variable calculation ###

Years = xr.concat((Years_01.Years_FC,Years_02.Years_FC,Years_03.Years_FC,Years_04.Years_FC,Years_05.Years_FC,Years_06.Years_FC,Years_07.Years_FC,Years_08.Years_FC,Years_09.Years_FC,Years_10.Years_FC,
				Years_11.Years_FC,Years_12.Years_FC,Years_13.Years_FC,Years_14.Years_FC,Years_15.Years_FC,Years_16.Years_FC,Years_17.Years_FC,Years_18.Years_FC,Years_19.Years_FC,Years_20.Years_FC,
				Years_21.Years_FC,Years_22.Years_FC),dim='model')

FC = xr.concat((FC_01.FC,FC_02.FC,FC_03.FC,FC_04.FC,FC_05.FC,FC_06.FC,FC_07.FC,FC_08.FC,FC_09.FC,FC_10.FC,
				FC_11.FC,FC_12.FC,FC_13.FC,FC_14.FC,FC_15.FC,FC_16.FC,FC_17.FC,FC_18.FC,FC_19.FC,FC_20.FC,
				FC_21.FC,FC_22.FC),dim='model')

PFS = xr.concat((PFS_01.PFS,PFS_02.PFS,PFS_03.PFS,PFS_04.PFS,PFS_05.PFS,PFS_06.PFS,PFS_07.PFS,PFS_08.PFS,PFS_09.PFS,PFS_10.PFS,
				PFS_11.PFS,PFS_12.PFS,PFS_13.PFS,PFS_14.PFS,PFS_15.PFS,PFS_16.PFS,PFS_17.PFS,PFS_18.PFS,PFS_19.PFS,PFS_20.PFS,
				PFS_21.PFS,PFS_22.PFS),dim='model')


Years_total = Years.mean(dim='model')

FC_total = xr.full_like(Years_total, fill_value=0)
FC_total = xr.where((KG==1) & (Years_total>=7.),11,FC_total)
FC_total = xr.where((KG==1) & (Years_total>3.) & (Years_total<7.),12,FC_total)
FC_total = xr.where((KG==1) & (Years_total>0.) & (Years_total<=3.),13,FC_total)
FC_total = xr.where((KG==2) & (Years_total>=7.),21,FC_total)
FC_total = xr.where((KG==2) & (Years_total>3.) & (Years_total<7.),22,FC_total)
FC_total = xr.where((KG==2) & (Years_total>0.) & (Years_total<=3.),23,FC_total)
FC_total = xr.where((KG==3) & (Years_total>=7.),31,FC_total)
FC_total = xr.where((KG==3) & (Years_total>3.) & (Years_total<7.),32,FC_total)
FC_total = xr.where((KG==3) & (Years_total>0.) & (Years_total<=3.),33,FC_total)
FC_total = xr.where((KG>=4) & (Years_total>=7.),41,FC_total)
FC_total = xr.where((KG>=4) & (Years_total>3.) & (Years_total<7.),42,FC_total)
FC_total = xr.where((KG>=4) & (Years_total>0.) & (Years_total<=3.),43,FC_total)

FC_class = FC.where(FC>0).count(dim='model')/22.*100.
FC_noclass = FC.where(FC==0).count(dim='model')/22.*100.

FC_total = xr.where((FC_class<50.) & (FC_total>0),0,FC_total)
FC_total.name='FC'
FC_total.to_netcdf('FC.nc')

FC_confidence = xr.full_like(FC_total, fill_value=0)
FC_confidence = xr.where(FC_total>0,FC_class,FC_confidence)
FC_confidence = xr.where(FC_total==0,FC_noclass,FC_confidence)
FC_confidence.name='FC_confidence'
FC_confidence.to_netcdf('FC_confidence.nc')

Years_total = xr.where(FC_total==0,0.,Years_total)
Years_total.name='Years_FC'
Years_total.to_netcdf('Years_FC.nc')

PFS = xr.where(FC==0,np.nan,PFS)
PFS_total = PFS.mean(dim='model',skipna=True)
PFS_total = xr.where(FC_total==0.,0.,PFS_total)
PFS_total.name='PFS'
PFS_total.to_netcdf('PFS.nc')

